﻿using PersistenceLayer;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zian.WinForm.Framework.Commons.Contexts;
using Zian.WinForm.Framework.Exceptions;
using Zian.WinForm.Framework.Orm;
using Zian.WinForm.Framework.Pages;

namespace ZhiQianWinform
{
    static class Program
    {
        /// <summary>
        /// 该函数设置由不同线程产生的窗口的显示状态  
        /// </summary>  
        /// <param name="hWnd">窗口句柄</param>  
        /// <param name="cmdShow">指定窗口如何显示。查看允许值列表</param>  
        /// <returns>如果窗口原来可见，返回值为非零；如果窗口原来被隐藏，返回值为零</returns>                      
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        private const int SW_SHOWNOMAL = 1;
        /// <summary>  
        ///  该函数将创建指定窗口的线程设置到前台，并且激活该窗口
        ///  系统给创建前台窗口的线程分配的权限稍高于其他线程。
        /// </summary>  
        /// <param name="hWnd">将被激活并被调入前台的窗口句柄</param>  
        /// <returns>如果窗口设入了前台，返回值为非零；如果窗口未被设入前台，返回值为零</returns>  
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // 判断是否重复运行 可不要
            Process currentProcess = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
            foreach (Process process in processes)
            {
                //如果实例已经存在,则忽略当前进程
                if (process.Id != currentProcess.Id)
                {
                    //保证要打开的进程 同 已经存在的进程来自同一个文件路径
                    if (Equals(process.MainModule.FileName, currentProcess.MainModule.FileName))
                    {
                        MessageBox.Show("已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //返回另一个进程实例
                        ShowWindowAsync(process.MainWindowHandle, SW_SHOWNOMAL);//调用API函数,正常显示窗口
                        SetForegroundWindow(process.MainWindowHandle);//将窗口放置在最前端  
                        return;
                    }
                }
            }
            // 日志初始化
            Logger.AppendLogger(WinFormDefaultLogger.GetInstance());
            // ORM 初始化
            OrmSettings.Init();
            // Setting setting = OrmSettings.Init();
            // setting.DatabaseMapFile = Path.Combine(Environment.CurrentDirectory, "Resources", "DatabaseMap.xml");
            // 异常初始化
            GlobalExceptionHandler.BindExceptionHandler();
            // 可以自定义登录框，继承 Form、UIForm、Page_Login 都可以
            using (Page_Login login = new Page_Login())
            {
                login.ShowDialog();
                if (login.DialogResult == DialogResult.OK)
                {
                    // 登录成功打开应用程序主窗体
                    login.Dispose();
                    UIForm FormMain = new Page_Main();
                    Application.Run(new ZianApplicationContext(FormMain));
                }
                else if (login.DialogResult == DialogResult.Cancel)
                {
                    login.Dispose();
                    return;
                }
            }
        }
    }
}
